//
//  HOTResetPwdViewController.m
//  Genpara
//
//  Created by zhang chunhui on 14-9-30.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTResetPwdViewController.h"
#import "ZZZUIScrollView+Extends.h"
#import "UITextField+extend.h"

@interface HOTResetPwdViewController () <UITextFieldDelegate>

@end

@implementation HOTResetPwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self setNaviTitle:@"パスワード再設定" fontSize:20 fontName:HOTTitleFont_ROUNDED_X];
    
    [self addRightBtnTarget:self action:@selector(saveBtnClicked) image:[UIImage imageNamed:@"btn_modification_off"] hImage:[UIImage imageNamed:@"btn_modification_on"]];

}

-(void)saveBtnClicked {
    
    if (![self checkParameters]) {
        return;
    }
    
    [self doSave];
}

-(BOOL)checkParameters {
    
    if ([_oldPwdTex textIsNullOrEmpty]) {
        
        NSString *errMsg = [self getErrMsgWith:@"現在のパスワード" itemType:HOTInputItemText];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:errMsg
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        return NO;
    } else if ([_newPwdText textIsNullOrEmpty]) {
        
        NSString *errMsg = [self getErrMsgWith:@"新しいパスワード" itemType:HOTInputItemText];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:errMsg
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        return NO;
    } else if ([_confirmPwdText textIsNullOrEmpty]) {
        
        NSString *errMsg = [self getErrMsgWith:@"パスワード（確認用）" itemType:HOTInputItemText];
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:errMsg
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        return NO;
    }
    
    if (![_confirmPwdText.text isEqualToString:_newPwdText.text]) {
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:MSG_PWD_NO_SAME
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        return NO;
    }
    
    if ([_oldPwdTex.text isEqualToString:_newPwdText.text]) {
        
        ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                              message:MSG_PWD_VALID
                                                             delegate:nil
                                                    cancelButtonTitle:nil
                                                     otherButtonTitle:ALERT_BTN_OK];
        
        [popupView showInWindow];
        return NO;
    }
    
    return YES;
}

//保存
-(void)doSave {
    
    [self showLoading];
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_PWD_OLD:_oldPwdTex.text,
                                 REQ_KEY_PWD_NEW:_newPwdText.text};
    
    __unsafe_unretained HOTResetPwdViewController *tempObj = self;
    [self requestWithURL:HOT_URL_CHANGE_PWD Parameters:parameters success:^(NSInteger result, NSArray * data) {
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^(void) {
            
            [tempObj.navigationController popViewControllerAnimated:YES];
        }];
    } failure:nil];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    
    [_mainScrollView scrollViewToVisible:textField animated:YES];
    
    return YES;
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    
    [_mainScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    
    return YES;
}

-(void)hiddenKeyBoard {
    
    [_newPwdText resignFirstResponder];
    [_oldPwdTex resignFirstResponder];
    [_confirmPwdText resignFirstResponder];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
